﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.MODEL.Statistics;
using MINERP.DAL;

namespace MINERP.BLL.Statistics
{
    public class AlarmMessageBLL
    {
        public List<AlarmMessageInfo> GetAlarmMessageInfoList(int readuid)
        {
            List<AlarmMessageInfo> list = new List<AlarmMessageInfo>();
            List<AccountAlarmMsg> alist;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {

                alist = entities.AccountAlarmMsg.Include("Shop").Where(a => a.ReaderUid == readuid&&a.Status ==0).OrderByDescending(a => a.AlarmTime).ToList();
                foreach (var item in alist)
                {
                    AlarmMessageInfo alarm = new AlarmMessageInfo();

                    alarm.Id = item.Id;
                    alarm.AlarmTime = item.AlarmTime;
                    alarm.Status = item.Status;
                    alarm.Remark = item.Remark;
                    alarm.StatusName = "未读";

                    users user = entities.users.FirstOrDefault(u => u.userID == item.SenderUid);
                    if (user != null)
                    {
                        alarm.Sender = user.UserName;
                    }
                    else
                    {
                        alarm.Sender = "系统";
                    }
                    alarm.Msg = item.Msg;

                    alarm.AlarmDays = item.Shop.AlarmDays;
                    alarm.AlarmLimit = item.Shop.AlarmLimit;
                    alarm.Balance = (decimal)item.Shop.Balance;
                    alarm.CreditLimit = (decimal)item.Shop.CreditLimit;
                    alarm.OrderBalance = item.Shop.OrderBalance == null ? 0 : (decimal)item.Shop.OrderBalance;
                    alarm.ShopName = item.Shop.shopName;
                    alarm.ShopTel = item.Shop.Tel1;
                    list.Add(alarm);
                }
            }

            return list;
        }

        /// <summary>
        /// 是否有该用户未读消息
        /// </summary>
        /// <param name="readuid"></param>
        /// <returns></returns>
        public bool HaveAlarmMessage(int readuid)
        {
            bool flag = false;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                int num = entities.AccountAlarmMsg.Where(a => a.ReaderUid == readuid&&a.Status ==0).ToList().Count;
                if (num > 0)
                {
                    flag = true;
                }
            }
            return flag;
        }

        /// <summary>
        /// 按条件查询Alarms
        /// </summary>
        /// <param name="readuid"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<AlarmMessageInfo> Select(int readuid,int status,DateTime start,DateTime end)
        {
            List<AlarmMessageInfo> list = new List<AlarmMessageInfo>();
            List<AccountAlarmMsg> alist;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {

                alist = entities.AccountAlarmMsg.Include("Shop").Where(a => a.ReaderUid == readuid && a.Status == status&&a.AlarmTime>=start&&a.AlarmTime<=end).OrderByDescending(a => a.AlarmTime).ToList();
                
                foreach (var item in alist)
                {
                    AlarmMessageInfo alarm = new AlarmMessageInfo();

                    alarm.Id = item.Id;
                    alarm.AlarmTime = item.AlarmTime;
                    alarm.Status = item.Status;
                    if (item.Status == 0)
                    {
                        alarm.StatusName = "未读";
                    }
                    else
                    {
                        alarm.StatusName = "已读";
                    }
                    users user = entities.users.FirstOrDefault(u => u.userID == item.SenderUid);
                    if (user != null)
                    {
                        alarm.Sender = user.UserName;
                    }
                    else
                    {
                        alarm.Sender = "系统";
                    }
                    alarm.Msg = item.Msg;

                    alarm.AlarmDays = item.Shop.AlarmDays;
                    alarm.AlarmLimit = item.Shop.AlarmLimit;
                    alarm.Balance = (decimal)item.Shop.Balance;
                    alarm.CreditLimit = (decimal)item.Shop.CreditLimit;
                    alarm.OrderBalance = (decimal)item.Shop.OrderBalance;
                    alarm.ShopName = item.Shop.shopName;
                    alarm.ShopTel = item.Shop.Tel1;
                    list.Add(alarm);
                }
            }

            return list;
        }

        /// <summary>
        /// 标记为已读
        /// </summary>
        /// <param name="id"></param>
        public void Update(int id)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                AccountAlarmMsg alarm = entities.AccountAlarmMsg.FirstOrDefault(a => a.Id == id);
                
                if (alarm != null)
                {
                    List<AccountAlarmMsg> list = entities.AccountAlarmMsg.Where(a => a.Remark == alarm.Remark).ToList();
                    foreach (var item in list)
                    {
                        item.Status = 1;
                    }
                    //alarm.Status = 1;
                }
                entities.SaveChanges();
            }
        }
    }
}
